package com.txhy.certificatechain.ui.activity.pay;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.google.zxing.Result;
import com.txhy.certificatechain.R;
import com.txhy.certificatechain.model.bean.UserBean;
import com.txhy.certificatechain.presenter.pay.ScannerPresenter;
import com.txhy.certificatechain.ui.activity.base.BaseActivity;
import com.txhy.certificatechain.ui.view.pay.ScannerView;
import com.txhy.certificatechain.utils.PrefUtil;
import com.txhy.certificatechain.utils.ToastUtil;
import com.zhangke.qrcodeview.QRCodeView;

import butterknife.BindView;
import butterknife.OnClick;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: ScannerActivity.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 扫一扫
 * 创建时间: 2018/9/21 10:42
 * 更新时间：2018/9/21 10:42
 * -----------------------------------------------------------------
 * 2018/9/21 10:42 : 创建 ScannerActivity.java (侯建军);
 * -----------------------------------------------------------------
 */
public class ScannerActivity extends BaseActivity<ScannerView, ScannerPresenter> implements ScannerView {
    @BindView(R.id.qr_code_view)
    QRCodeView qr_code_view;

    @BindView(R.id.im_left)
    ImageView im_left;

    private UserBean.DataEntity.MapEntity user;

    @Override
    protected ScannerPresenter createPresenter() {
        return new ScannerPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_scanner;
    }

    private int scanner;

    @Override
    public void initView() {
        super.initView();
        user = PrefUtil.getUserBean(ScannerActivity.this);

        qr_code_view.setOnQRCodeListener(new QRCodeView.OnQRCodeRecognitionListener() {
            @Override
            public void onQRCodeRecognition(Result result) {
                try {
                    if (scanner == 0) {
                        scanner++;
                        String[] type = result.getText().split("#");
                        String qrCode = type[0];
                        String otherId = type[1];
                        String payType = type[2];
                        //扫描类型
                        switch (payType) {
                            case "PAYIN"://带金额的
                            case "PAYINAMOUNT"://不带金额的
                                String belongType = type[3];
                                String money = type[4];
                                String reveReason = type[5];
                                Intent intent = new Intent(ScannerActivity.this, TransferActivity.class);
                                intent.putExtra("payType", payType);
                                intent.putExtra("otherId", otherId);
                                intent.putExtra("qrCode", qrCode);
                                intent.putExtra("belongType", belongType);
                                intent.putExtra("money", money);
                                intent.putExtra("reveReason", reveReason);
                                startActivity(intent);
                                finish();
                                break;
                            //扫描身份证
                            case "ECARD":
                                Intent intent1 = new Intent(ScannerActivity.this, ScannerResultActivity.class);
                                intent1.putExtra("type", 0);
                                intent1.putExtra("code", qrCode);
                                startActivity(intent1);
                                finish();
                                break;
                                //扫描证照
                            case "ELICENSE":
                                Intent intent2 = new Intent(ScannerActivity.this, ScannerResultActivity.class);
                                intent2.putExtra("code", qrCode);
                                intent2.putExtra("type", 1);
                                startActivity(intent2);
                                finish();
                                break;
                            //前台登录
                            case "LOGINFRONT":
                                mPresenter.webLogin(qrCode,user.getId()+"",user.getUserType() );
                                break;
                        }
                    }
                } catch (Exception e) {
                    ToastUtil.showLong(ScannerActivity.this, "无效二维码");
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        qr_code_view.startPreview();
    }
    @Override
    public void onPause() {
        qr_code_view.stopPreview();
        super.onPause();
    }
    @OnClick({R.id.im_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.im_left:
                finish();
                break;
        }
    }
    @Override
    public void webLoginSuccess(String data) {
        ToastUtil.showLong(ScannerActivity.this,data);
        finish();
    }
    @Override
    public void webLoginFail(String errorString) {
        ToastUtil.showLong(ScannerActivity.this,errorString);
    }
}

